package Algorithm.LinkedListProblem;

/**
 * @Author cj
 * @Date 2022/1/9 19:42
 */
public class insertNum {

    static class Node {
        public int value;
        public Node next;

        public Node(int val) {
            this.value = val;
        }
    }

    public static Node insertNum1(Node head, int num) {
        Node node = new Node(num);
        if (head == null) {
            node.next = node;
            return node;
        }
        Node pre = head, cur = head.next;
        while (cur != head) {
            if (pre.value <= num && num <= cur.value) {
                break;
            }
            pre = cur;
            cur = cur.next;
        }
        pre.next = node;
        node.next = cur;
        return num < head.value ? node : head;
    }
}
